import React, {Component, useEffect} from 'react';
import {Text, TextInput, StyleSheet, LogBox} from 'react-native';
import {Provider} from 'react-redux';
import store from './redux/store';
import {setRoutersStack} from './redux/action/index';
import Navigator from './navigation';
import './components/OverLayer';
import JPush from 'jpush-react-native';
import './utils/LocalServer';
// import QRCodeModal from '@walletconnect/qrcode-modal';
import WalletConnectProvider from '@walletconnect/react-native-dapp';
import AsyncStorage from '@react-native-async-storage/async-storage';
const SCHEME_FROM_APP_JSON = 'siginclub';

LogBox.ignoreAllLogs(true); //关闭警告提示
// console.disableYellowBox = true;//关闭警告提示

TextInput.defaultProps = Object.assign({}, TextInput.defaultProps, {
  allowFontScaling: false,
});
Text.defaultProps = Object.assign({}, Text.defaultProps, {
  allowFontScaling: false,
});

// 控制是否屏蔽全局console.log
const isDebug = true;
console.log = (function (oldLogFunc) {
  return function () {
    if (isDebug) {
      oldLogFunc.apply(this, arguments);
    }
  };
})(console.log);

// 控制是否屏蔽全局console.warn 日志
const showWarnLog = false;
console.warn = (function (oldLogFunc) {
  return function () {
    if (showWarnLog) {
      oldLogFunc.apply(this, arguments);
    }
  };
})(console.warn);

// 给全局文本组件添加空字体，fix在某些android机型上，添加fontWeight导致字体隐藏的问题
// const styles = StyleSheet.create({
//   defaultFontFamily: {
//     // fontFamily: 'system font',
//   },
// });

// Text.render = lodash.wrap(Text.render, (func, ...args) => {
//   const originText = func.apply(this, args);
//   return React.cloneElement(originText, {
//     style: [originText.props.style, styles.defaultFontFamily],
//   });
// });

function getCurrentRouteName(navigationState) {
  if (!navigationState) {
    return null;
  }
  const route = navigationState.routes[navigationState.index];
  // dive into nested navigators
  if (route.routes) {
    return getCurrentRouteName(route);
  }
  return route.routeName;
}

export default function App() {
  useEffect(() => {
    JPush.init();
    //连接状态
    this.connectListener = (result) => {
      console.log('connectListener:' + JSON.stringify(result));
    };
    JPush.addConnectEventListener(this.connectListener);
    //通知回调
    this.notificationListener = (result) => {
      console.log('notificationListener:' + JSON.stringify(result));
    };
    JPush.addNotificationListener(this.notificationListener);
    //本地通知回调
    this.localNotificationListener = (result) => {
      console.log('localNotificationListener:' + JSON.stringify(result));
    };
    JPush.addLocalNotificationListener(this.localNotificationListener);
    //自定义消息回调
    // this.customMessageListener = (result) => {
    //   console.log('customMessageListener:' + JSON.stringify(result));
    // };
    // JPush.addCustomMessagegListener(this.customMessageListener);
    //tag alias事件回调
    this.tagAliasListener = (result) => {
      console.log('tagAliasListener:' + JSON.stringify(result));
    };
    JPush.addTagAliasListener(this.tagAliasListener);
    //手机号码事件回调
    this.mobileNumberListener = (result) => {
      console.log('mobileNumberListener:' + JSON.stringify(result));
    };
    JPush.addMobileNumberListener(this.mobileNumberListener);
  });

  return (
    <WalletConnectProvider
      redirectUrl={
        Platform.OS === 'web'
          ? window.location.origin
          : `${SCHEME_FROM_APP_JSON}://`
      }
      storageOptions={{
        asyncStorage: AsyncStorage,
      }}>
      <Provider store={store}>
        <Navigator
          onNavigationStateChange={(prevState, currentState) => {
            const currentScreen = getCurrentRouteName(currentState);
            const prevScreen = getCurrentRouteName(prevState);
            console.log(`离开${prevScreen}页面,进入${currentScreen}页面`);
            console.log(JSON.stringify(currentState));
            store.dispatch(setRoutersStack(currentState));
          }}
        />
      </Provider>
    </WalletConnectProvider>
  );
}
// export default class App extends Component {
//   componentDidMount() {
//     JPush.init();
//     //连接状态
//     this.connectListener = (result) => {
//       console.log('connectListener:' + JSON.stringify(result));
//     };
//     JPush.addConnectEventListener(this.connectListener);
//     //通知回调
//     this.notificationListener = (result) => {
//       console.log('notificationListener:' + JSON.stringify(result));
//     };
//     JPush.addNotificationListener(this.notificationListener);
//     //本地通知回调
//     this.localNotificationListener = (result) => {
//       console.log('localNotificationListener:' + JSON.stringify(result));
//     };
//     JPush.addLocalNotificationListener(this.localNotificationListener);
//     //自定义消息回调
//     // this.customMessageListener = (result) => {
//     //   console.log('customMessageListener:' + JSON.stringify(result));
//     // };
//     // JPush.addCustomMessagegListener(this.customMessageListener);
//     //tag alias事件回调
//     this.tagAliasListener = (result) => {
//       console.log('tagAliasListener:' + JSON.stringify(result));
//     };
//     JPush.addTagAliasListener(this.tagAliasListener);
//     //手机号码事件回调
//     this.mobileNumberListener = (result) => {
//       console.log('mobileNumberListener:' + JSON.stringify(result));
//     };
//     JPush.addMobileNumberListener(this.mobileNumberListener);
//   }

//   render() {
//     return (
//       <Provider store={store}>
//         <Navigator
//           onNavigationStateChange={(prevState, currentState) => {
//             const currentScreen = getCurrentRouteName(currentState);
//             const prevScreen = getCurrentRouteName(prevState);
//             console.log(`离开${prevScreen}页面,进入${currentScreen}页面`);
//             console.log(JSON.stringify(currentState));
//             store.dispatch(setRoutersStack(currentState));
//           }}
//         />
//       </Provider>
//     );
//   }
// }
